package com.ruoyi.grade.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.grade.domain.entity.MsUser;
import com.ruoyi.grade.domain.entity.MsUserRole;
import com.ruoyi.grade.domain.entity.Student;
import com.ruoyi.grade.mapper.MsUserMapper;
import com.ruoyi.grade.mapper.MsUserRoleMapper;
import com.ruoyi.grade.mapper.StudentMapper;
import com.ruoyi.grade.service.IMsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MsUserServiceImpl extends ServiceImpl<MsUserMapper, MsUser>
implements IMsUserService {

    @Autowired
    private MsUserMapper userMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private MsUserRoleMapper userRoleMapper;

    @Override
    public String importUser(List<MsUser> userList, Boolean updateSupport, String operName) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (MsUser user : userList) {
            //判断账号是否存在
            if(!checkUserNameExist(user.getUserName())){
                //账号不存在
                user.setNickName(user.getUserName());
                user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
                user.setCreateBy(operName);

                //调用保存业务
                userMapper.insert(user);
                studentMapper.insert(new Student(user.getUserName(), user.getPassword()));
                userRoleMapper.insert(new MsUserRole(user.getUserId(), 100l));

                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
            } else if (updateSupport){
                //账号存在并同意更新
                user.setUserId(getUserIdByUserName(user.getUserName()));
                user.setNickName(user.getUserName());
                user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
                user.setCreateBy(operName);

                //调用修改业务
                userMapper.updateById(user);
                studentMapper.updateById(new Student(user.getUserName(), user.getPassword()));

                successNum++;
                successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
            } else{
                //账号存在且不支持更新
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return failureNum == 0? failureMsg.toString(): successMsg.toString();
    }

    @Override
    public Boolean checkUserNameExist(String userName) {
        //设置查询条件
        LambdaQueryWrapper<MsUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MsUser::getUserName, userName);
        lqw.eq(MsUser::getDelFlag, "0");
        //调用查询业务
        return userMapper.selectOne(lqw) != null;
    }

    @Override
    public Long getUserIdByUserName(String userName) {
        //设置查询条件
        LambdaQueryWrapper<MsUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MsUser::getUserName, userName);
        lqw.eq(MsUser::getDelFlag, "0");
        //调用查询业务
        return userMapper.selectOne(lqw).getUserId();
    }
}
